package common.impl;

import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

import common.interf.IPropertyHandle;

public class PropertyHandleImpl implements IPropertyHandle{
	private Properties property = new Properties();
	private String propertyFile = "";

	/**
	 * 初始化property
	 */
	private void initProperty() {
		try {
			if(StringUtils.isEmpty(propertyFile))
				return;
			URL url = getClass().getResource(propertyFile);
			InputStream is = url.openStream();
			property.clear();
			property.load(is);
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 初始化
	 */
	public PropertyHandleImpl() {
		initProperty();
	}

	/**
	 * @param key 取得其值的键
	 * @return key的值
	 */
	public String getValue(String key) {
		
		if (property.containsKey(key)) {
			String value = property.getProperty(key);// 得到某一属性的值
			return value;
		} else
			return "";
	}

	/**
	 * 设置文件路径
	 */
	public void setPropertyFile(String filePath) {
		if (StringUtils.isEmpty(filePath))
			return;
		this.propertyFile = filePath;
		initProperty();
	}
}